package com.lite.business.service.commodities.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lite.business.convert.commodities.CommoditiesConvert;
import com.lite.business.entity.commodities.Commodities;
import com.lite.business.entity.commodities.CommodityQueryWrapper;
import com.lite.business.exception.BusinessException;
import com.lite.business.mapper.commodities.CommoditiesMapper;
import com.lite.business.service.commodities.CommoditiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author stranger
 * @since 2022-10-25
 */
@Service
public class CommoditiesServiceImpl extends ServiceImpl<CommoditiesMapper, Commodities> implements CommoditiesService {


    @Autowired
    CommoditiesMapper commoditiesMapper;

    @Autowired
    CommoditiesConvert commoditiesConvert;

    @Override
    @Cacheable(value = "getCommodityById", key = "#cmodId")
    public Commodities getCommodityById(Integer cmodId) throws BusinessException {
        Commodities commodity = commoditiesMapper.selectOne(new LambdaQueryWrapper<Commodities>().eq(Commodities::getId, cmodId));
        if (Objects.isNull(commodity)) {
            throw new BusinessException("该商品不存在");
        }
        return commodity;
    }

    @Override
    @Cacheable("listCommoditiesByPage")
    public List<Commodities> listCommoditiesByPage(Integer page, Integer size, CommodityQueryWrapper commodityQueryWrapper) {
        //分页查询
        Page<Commodities> commoditiesPageRes = this.page(new Page<>(page, size), commodityQueryWrapper.buildWrapper());

        return commoditiesPageRes.getRecords();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getCommodityById", key = "#cmodId"),
                    @CacheEvict(value = "listCommoditiesByPage", allEntries = true)
            }
    )
    public Boolean updateCommoditiesById(Integer cmodId, Commodities newCommodity) throws BusinessException {
        int count = commoditiesMapper.updateById(newCommodity);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "商品信息更新失败");
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getCommodityById", key = "#cmodId"),
                    @CacheEvict(value = "listCommoditiesByPage", allEntries = true)
            }
    )
    public Boolean removeCommoditiesById(Integer cmodId) throws BusinessException {
        int count = commoditiesMapper.delete(new LambdaQueryWrapper<Commodities>().eq(Commodities::getId, cmodId));
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "商品删除失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getCommodityById", key = "#commodities.id"),
                    @CacheEvict(value = "listCommoditiesByPage", allEntries = true)
            }
    )
    public Boolean addCommodities(Commodities commodities) throws BusinessException {

        int count = commoditiesMapper.insert(commodities);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "新增商品失败");
        }

        return true;
    }
}
